//
//  ViewController.swift
//  SNPTest
//
//  Created by luoyuan on 2017/4/6.
//  Copyright © 2017年 luoyuan. All rights reserved.
//

import UIKit
import SnapKit
import AVFoundation

protocol Delegate : NSObjectProtocol {
    func touchme(value: CGFloat) -> CGFloat;
}

class ViewController: UIViewController, Delegate {

    fileprivate weak var delegate: Delegate?;
    fileprivate var name: String? {
        willSet {
        
        }
        didSet {
        
        }
    };
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self;
        
        let value: CGFloat = self.delegate?.touchme(value: 12) ?? 0;
        
        print("打印");
        print(value);
        
        self.view.backgroundColor = UIColor.gray;
        let view:UIWebView = UIWebView();
        view.backgroundColor = UIColor.yellow;
        
        let url:URL = URL.init(string: "www.baidu.com")!;
        let request:URLRequest = URLRequest.init(url: url);
        
        view.loadRequest(request);
        
        self.view.addSubview(view);
        view.backgroundColor = UIColor.red;
        view.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsetsMake(0, 0, 100, 100));
        };
        
        let layer:CAShapeLayer = CAShapeLayer();
        
        let path:UIBezierPath = UIBezierPath();
        let pointA:CGPoint = CGPoint.init(x: 200, y: 200);
        let pointB:CGPoint = CGPoint.init(x: pointA.x - 100, y: 300);
        let pointC:CGPoint = CGPoint.init(x: pointA.x + 100, y: 300);
        
        path.move(to: pointA)
        path.addLine(to: pointB);
        path.addLine(to: pointC);
        path.addLine(to: pointA);
        
        path.close();
    
        layer.path = path.cgPath;
        
        layer.fillColor = UIColor.blue.cgColor;
        

        
        self.view.layer.addSublayer(layer);
        
        let player:AVPlayer = AVPlayer.init(url: URL.init(string: "http://www.dafuimg.com/mp3/Voice/dlh.mp3")!);
        player.volume = 1;
        
//        let playerItem = AVPlayerItem.init(url: URL.init(string: "http://www.dafuimg.com/mp3/Voice/dlh.mp3")!)
//        player.replaceCurrentItem(with: playerItem);
        
        player.play();
        
        let imageView: UIImageView = UIImageView.init(frame: CGRect.init(x: 200, y: 300, width: 44, height: 44));
        imageView.image = UIImage.init(named: "logo1024");
        self.view.addSubview(imageView);
        imageView.isUserInteractionEnabled = true;
        imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(click(_:))));
        
        let dic: [String: Any] = ["key1" : 22, "key2" : "22"];
        let model: Model? = Model.deserialize(from: dic);
        print("解析")
        let str: String = "{\"result\":\"true\",\"errorCode\":\"\",\"errorMsg\":\"\",\"data\":[{\"objectIntValue\":20170808,\"objectStringValue\":\"https://www.f598.com/images/Default/mobile/app.png\",\"objectKey\":\"AppStartImg\"},{\"objectIntValue\":20171031,\"objectStringValue\":\"\",\"objectKey\":\"AppIntroImg\"}]}";
        let model1: GetAppInfoModel? = GetAppInfoModel.deserialize(from: str);
        print(model?.key1 ?? "");
        print(model1?.data?.first?.objectIntValue ?? "")
//        var name = 0;
//
//        let key1: Int! = dic["key1"] as! Int;
//        let key2: String! = dic["key2"] as! String;
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc fileprivate func click(_ tap: UITapGestureRecognizer) {
        let view: UIView? = tap.view;
        let bounceAnimation: CAKeyframeAnimation = CAKeyframeAnimation.init(keyPath: "transform.scale");
        bounceAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0];
        bounceAnimation.duration = 0.5;
        bounceAnimation.calculationMode = kCAAnimationCubic;
        view?.layer.add(bounceAnimation, forKey: "bounceAnimation")
    }

    func touchme(value: CGFloat) -> CGFloat {
        print("传值");
        print(value);
        return 22;
    }
    

}

